/*
 * readn.c - read exactly n bytes
 *
 *  Created on: Jun 1, 2014
 *      Author: Lhmily
 */
#include "../include/base.h"
int readn(SOCKET fd, char *bp, size_t len) {
	int cnt, rc;
	cnt = len;
	while (cnt > 0) {
		rc = recv(fd, bp, cnt, 0);
		if (rc < 0) {
			if (EINTR == errno)
				continue;
			return -1;
		}
		if (0 == rc)
			return len - cnt;
		bp += rc;
		cnt -= rc;
	}
	return len;
}
